//
//  DrugCommentViewController.m
//  安好
//
//  Created by lianchuang on 14/12/10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DrugCommentViewController.h"
#import "DrugCommentViewCell.h"
#import "AddDrugCommentViewController.h"
@interface DrugCommentViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation DrugCommentViewController
{
    NSInteger page;
    NSMutableArray *dataArray;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self loadCommentList];
}
- (void)loadCommentList
{
    page=page+1;
    NSString * postURL = [NSString stringWithFormat:@"%@wiki/drugComment",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters=@{
                               @"platform":kPlatform,
                               @"version":kVersion,
                               @"drugid":self.drugid,
                               @"page":[NSString stringWithFormat:@"%d",page],
                               };
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
        [self.tableview headerEndRefreshing];
        if ([status isEqualToString:@"1"]) {
            
            if ([[responseObject valueForKey:@"data"] count]<=0) {
                
            }else
            {
                for (int i=0; i<[[responseObject valueForKey:@"data"] count]; i++) {
                    [dataArray addObject:[[responseObject valueForKey:@"data"] objectAtIndex:i]];
                }
            }
            [self.tableview reloadData];
        }
        else
        {
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableview headerEndRefreshing];
        [self showCustomHudSingleLine:@"暂无数据"];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"点评";
    self.tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStyleGrouped];
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    [self.tableview addHeaderWithTarget:self action:@selector(loadCommentList)];
    [self.tableview addFooterWithTarget:self action:@selector(loadCommentList)];

//    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    page=0;
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackDown)];
    backBtnItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    dataArray=[[NSMutableArray alloc]init];
    
    UIButton *addComment=[UIButton buttonWithType:UIButtonTypeCustom];
    addComment.frame=CGRectMake(15, kScreenHeight-64-55, kScreenWidth - 30, 47);
    [addComment setBackgroundColor:[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16]];
    [addComment addTarget:self action:@selector(doadd:) forControlEvents:UIControlEventTouchUpInside];
    addComment.titleLabel.font=[UIFont systemFontOfSize:15];
    [addComment setTitle:@"添加评论" forState:UIControlStateNormal];
    [self.view addSubview:addComment];
}
- (void)leftBackDown
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)doadd:(UIButton *)sender
{
    NSString * userIsLogin =  [NSString stringWithFormat:@"%@",[[UserManager shareInstance] getUserIsLogin]];
    if ([userIsLogin length] == 0 || [[UserManager shareInstance]getUserIsLogin ] == nil) {
        [self isLogin];
    }
    else
    {
        AddDrugCommentViewController *addcomment=[[AddDrugCommentViewController alloc]initWithNibName:@"AddDrugCommentViewController" bundle:[NSBundle mainBundle]];
        addcomment.drugid=self.drugid;
        [self.navigationController pushViewController:addcomment animated:YES];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier=@"cellcomment";
    DrugCommentViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"DrugCommentViewCell" owner:self options:nil] lastObject];
    }
    if (dataArray.count>0) {
        NSDictionary *dic=[dataArray objectAtIndex:indexPath.row];
        cell.dateline.text=[dic valueForKey:@"dateline"];
        [cell.headImage setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        cell.username.text=[dic valueForKey:@"username"];
        cell.content.text=[dic valueForKey:@"comment"];
        if (indexPath.row==0) {
            cell.bottomview.frame=CGRectMake(55, cell.topview.top, 280, 0.5);
        }else if (indexPath.row<=dataArray.count-2)
        {
            cell.topview.hidden=YES;
            cell.bottomview.hidden=NO;
            cell.bottomview.frame=CGRectMake(55, cell.content.bottom, cell.bottomview.width, 0.5);
        }
        else if (indexPath.row==dataArray.count-1)
        {
            cell.topview.hidden=YES;
        }
        [self autosizeFontAndSize:cell.content withText:cell.content.text];
        cell.bottomview.frame=CGRectMake(cell.bottomview.left, cell.content.bottom+10, cell.bottomview.width, 0.5);
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrugCommentViewCell *cell=(DrugCommentViewCell *)[self tableView:self.tableview cellForRowAtIndexPath:indexPath];
    return cell.content.bottom+10;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark Lable自适应高度
- (void)autosizeFontAndSize:(UILabel *)label withText:(NSString *)text
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    label.attributedText = attributedString;
    [label sizeToFit];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
